#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
__title__ = ''
__author__ = 'ben'
__mtime__ = '4/28/16'
"""

class CDataBaseUrl(object):
    def __init__(self, db_type='mysql'):
        self.db_type = db_type

    def make_db_url(self, db_uri=None):
        if not db_uri or not isinstance(db_uri,dict):
            return

        host,port,db_name,user,password,db_path = '','','','','',''
        if self.db_type != 'sqlite':
            host = db_uri.get('host')
            port = int(db_uri.get('port'))
            db_name = db_uri.get('database')
            user = db_uri.get('user')
            password = db_uri.get('password')
        else:
            db_path = db_uri.get('database')

        # dialect+driver://username:password@host:port/database
        if self.db_type == 'mysql':
            db_url = 'mysql+mysqldb://%s:%s@%s:%d/%s?charset=utf8' % (user, password, host, port, db_name)
        elif self.db_type == 'postgresql':
            db_url = 'postgresql+psycopg2://%s:%s@%s:%d/%s?charset=utf8' % (user, password, host, port, db_name)
        elif self.db_type == 'sqlite':
            db_url = 'sqlite:///%s?charset=utf8' % (db_path)
        elif self.db_type == 'oracle':
            db_url = 'oracle://%s:%s@%s:%d/%s?charset=utf8' % (user, password, host, port, db_name)
        elif self.db_type == 'sqlserver':
            db_url = 'mssql+pymssql://%s:%s@%s:%d/%s?charset=utf8' % (user, password, host, port, db_name)
        else:
            return

        return db_url
